<!DOCTYPE html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>${name_china}管理列表</title>
<%@include file="/common/head.jsp" %>

<!-- 时间日期插件 -->
<link href="<%=request.getContextPath()%>/static/plugin/My97DatePicker/skin/default/datepicker.css" rel="stylesheet" >
<script src="<%=request.getContextPath()%>/static/plugin/My97DatePicker/WdatePicker.js"></script>
<style>
.maskback{
	position:fixed;
	width:100%;
	height:100%;
	top:0px;
	background:rgba(0,0,0,0.7);
}
.mainback{
    width: 40%;
    height: 200px;
    background: #fff;
    margin: 0 auto;
    margin-top: 17%;
}
</style>
</head>
<body class="iframe-page">
<div class="page-title-wrapper">
    <div class="page-title">
        <div class="page-title-text">
            <span>${name_china}管理</span>
        </div>
        <div id="toolbar" style="float: right"></div>
    </div>
    <div class="page-title-border"></div>
</div>

<div class="search-field">
    <span class="field-cont">
        <span class="field-cont-title">商品名称</span>
        <input type="text" class="summer-input" id="goodsName" name="goodsName"/>
    </span>
    
    <span class="field-cont">
        <span class="field-cont-title">是否爆款</span>
        <input type="text" class="summer-input" id="isBaokuan" name="isBaokuan"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">佣金排序</span>
        <input type="text" class="summer-input" id="commType" name="commType"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">佣金比例排序</span>
        <input type="text" class="summer-input" id="commTypePer" name="commTypePer"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">时间排序</span>
        <input type="text" class="summer-input" id="createTimeType" name="createTimeType"/>
    </span>
    
     <span class="field-cont">
        <span class="field-cont-title">是否有优惠商品</span>
        <input type="text" class="summer-input" id="isCoupon" name="isCoupon"/>
    </span>
    
    <span style="margin-left: 500px;" class="field-btn-wrapper">
        <a id="search" class="btn btn-primary btn-icon wave distance-right"><i class="icon icon-search"></i></a>
        <a id="reset" class="btn btn-icon wave wave-dark"><i class="icon icon-refresh"></i></a>
    </span>
</div>

<div id="t-wrap">
    <table class="summer-datagrid" id="dataList">
	    <thead>
		    <tr>
		        <th>排序</th>
		        <th>商品图片</th>
		        <th>商品名称</th>
		        <th>商品价格</th>
		        <th>用卷价格</th>
				<th>商品佣金</th>
				<th>商品佣金比例</th>
				<th>是否爆款</th>
				<th>是否推荐</th>
				<th>操作</th>
		    </tr>
	    </thead>
    </table>
</div>
<div style="display: none" class="maskback">
     <div class="mainback">
         <input type="hidden" name="reorderId" id="reorderId"/>
     	 <div class="form-cont" style="top:40px;">
	        <p class="form-cont-title"><span class="field-required">*</span>排序号</p>
	        <input type="text" id="reorder" name="reorder" class="summer-input summer-validatebox"  data-validatebox="validType:['required',noNegativeNumber]"/> 
	     </div>
	     <div class="form-btn-wrapper" id="funcbar" style="margin-top: 100px;">
	        <a id="save" href="javascript:;" class="btn btn-ok btn-normal  wave distance-right" data-id="b8efebfe7a21464fb0793800758ebb2a" data-url="/system/category/add" data-mark="create" data-permit="category:add">保存</a>
	        <a class="btn btn-normal" id="backBtn">关闭</a>
	     </div>
     </div>
</div>
<script>
	//请求url
	var CONST_URL = {
		LIST: basePath + '${common_url}list?categoryId=${categoryId}',
	   	INIT_ADD: basePath + "${common_url}initAdd?categoryId=${categoryId}",
	   	INIT_EDIT: basePath + "${common_url}initEdit?categoryId=${categoryId}",
	   	INIT_HONGBAO: basePath + "${common_url}initHongbao?categoryId=${categoryId}",
	   	INIT_ORDER: basePath + "${common_url}initOrder?categoryId=${categoryId}",
	   	INIT_BAOKUAN: basePath + "${common_url}initBaokuan?categoryId=${categoryId}",
		INIT_JINGXUAN: basePath + "${common_url}initJingxuan?categoryId=${categoryId}",
		DEL_BAOKUAN: basePath + "${common_url}delBaokuan",
		DEL_JINGXUAN: basePath + "${common_url}delJingxuan",
		SYNCPRODUCT: basePath + "${common_url}syncproduct",
		FILTERPRODUCT: basePath + "${common_url}filterproduct",
	   	DEL: basePath + "${common_url}del"
	};
	
    var  permission = CommonUtil.getPermission('${name_english}');
    
    $(function () {
    	//关闭按钮
    	$("#backBtn").click(function(){
    		$(".maskback").hide();
    	})
    	$("#save").on("click",function(){
    		var id = $("#reorderId").val();
    		var reorder = $("#reorder").val();
    		submitOrder(id,reorder);
    	});
    	
        // 新建toobar
		$.funcbar({
			id: 'toolbar',
			type: 'toolbar',
			data: permission,
			permit: ['${name_english}:filterproduct','${name_english}:syncproduct'],
		});
        
        // 查询按钮
        $('#search').on('click', reloadTable);
        
        //重置按钮
        $('#reset').on('click', function () {
        	//重置文本框的值
        	$('#goodsName').val('');
        	$("#isBaokuan").combobox("clear")
        	$("#commType").combobox("clear")
        	$("#commTypePer").combobox("clear")
        	$("#isCoupon").combobox("clear")
        	reloadTable();
        });
        
        $('#isBaokuan').combobox({
            valueField : 'code',
            textField : 'name',
            url: COMMON_URL.getCode + '?type=T_DM_COMMON_YESORNOT'
    	});
        
        $('#isCoupon').combobox({
            valueField : 'code',
            textField : 'name',
            url: COMMON_URL.getCode + '?type=T_DM_COMMON_YESORNOT'
    	});
        
        $('#commType').combobox({
            valueField : 'code',
            textField : 'name',
            url: COMMON_URL.getCode + '?type=T_DM_PRODUCT_JD_COMMTYPE'
    	});
        
        $('#commTypePer').combobox({
            valueField : 'code',
            textField : 'name',
            url: COMMON_URL.getCode + '?type=T_DM_PRODUCT_JD_COMMTYPE'
    	});
        
        $('#createTimeType').combobox({
            valueField : 'code',
            textField : 'name',
            url: COMMON_URL.getCode + '?type=T_DM_PRODUCT_JD_CREATE_TIME'
    	});
        
        //过滤
        $('#toolbar').on('click', 'a[data-permit="${name_english}:filterproduct"]', function() {
        	$.sdialog({
				type: 'confirm',
				msg: '您确定要过滤商品是否有效吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.FILTERPRODUCT,
			                beforeSend: function(){
			                	$.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '正在过滤中，请等待！'    
			                	});
			                },
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '已成功过滤！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});  	
		});
        
        //同步
        $('#toolbar').on('click', 'a[data-permit="${name_english}:syncproduct"]', function() {
        	$.sdialog({
				type: 'confirm',
				msg: '您确定要更新京东商品吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.SYNCPRODUCT,
			                beforeSend: function(){
			                	$.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '正在更新中，请等待！'    
			                	});
			                },
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '已成功同步！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        //新建
        $('#toolbar').on('click', 'a[data-permit="${name_english}:initAdd"]', function() {
        	window.location.href = CONST_URL.INIT_ADD;
		});
        
        
       createTable();
        
        
        //
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initOrder"]', function() {
        	var $this = $(this),
        	    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
        	//alert(id)
        	$(".maskback").show();
        	$("#reorderId").val(id);
        	$this.closest('tr').find('td[data-field="reorder"]').attr('id',id);
        });
       
        
        //编辑
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initEdit"]', function() {
            var $this = $(this),
                id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            window.location.href = CONST_URL.INIT_EDIT + '&id=' + id;
        });	
        
        
        //编辑
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initBaokuan"]', function() {
            var $this = $(this),
                id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            window.location.href = CONST_URL.INIT_BAOKUAN + '&id=' + id;
        });	
        
        //编辑
        $('#t-wrap').on('click', 'a[data-permit="${name_english}:initJingxuan"]', function() {
            var $this = $(this),
                id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
            window.location.href = CONST_URL.INIT_JINGXUAN + '&id=' + id;
        });	
        
        //删除
		$('#t-wrap').on('click', 'a[data-permit="${name_english}:del"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除选中的记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.DEL + "?id=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '删除成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
        
		//取消爆款
		$('#t-wrap').on('click', 'a[data-permit="${name_english}:delBaokuan"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要取消选中的爆款记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.DEL_BAOKUAN + "?id=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '取消成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
		
		//删除精选
		$('#t-wrap').on('click', 'a[data-permit="${name_english}:delJingxuan"]', function() {
			var $this = $(this),
			    id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
			$.sdialog({
				type: 'confirm',
				msg: '您确定要删除选中的推荐记录吗?',
				onConfirm: function(result) {
				    if (result) {
				    	$.sajax({
			                url : CONST_URL.DEL_JINGXUAN + "?id=" + id,
			                onLoadSuccess : function() {
			                	 $.stip({
                	                type:'ok',//显示类型 有info、 ok、 danger
                	                text: '取消成功！'    
			                	});
			                	reloadTable();  
			                }
			            });
				    		
				    }	
				}
			});
		});
        
    });
    
    // 重新加载表格
	function reloadTable() {
		$('#dataList').datagrid('reload',{
            ajax: {
            	param: {
            		"goodsName": trimSpace($('#goodsName').val()),
            		"isBaokuan": $("#isBaokuan").combobox("getValue"),
            		"commType": $("#commType").combobox("getValue"),
            		"commTypePer": $("#commTypePer").combobox("getValue"),
            		"isCoupon": $("#isCoupon").combobox("getValue"),
            		"createTimeType": $("#createTimeType").combobox("getValue")
            	}
            }
        });
	}
    
    //创建表格
    function createTable() {
    	$('#dataList').datagrid({
    		ajax: {
    			url: CONST_URL.LIST,
    			param: {
    				"goodsName": trimSpace($('#goodsName').val()),
            		"isBaokuan": $("#isBaokuan").combobox("getValue"),
            		"commType": $("#commType").combobox("getValue"),
            		"commTypePer": $("#commTypePer").combobox("getValue"),
            		"isCoupon": $("#isCoupon").combobox("getValue"),
            		"createTimeType": $("#createTimeType").combobox("getValue")
            	}
    		},
    		columns: [
    		     {
    		    	 field: 'id',
    		    	 hidden: true
    		     },
    		     {
    		    	 field: 'reorder',
    		    	 render: function (rowData) {
    		    		 if(rowData.reorder){
    		    			 return rowData.reorder;
    		    		 }else{
    		    			 return "无排序号";
    		    		 }
    		    	 }
    		     },
    		     {
    		    	 field: 'goodsUrl',
    		    	 render: function (rowData) {
    		    		 return '<img style="height:100px;width:100px;" src="'+ rowData.goodsUrl +'"></img>';
    		    	 }
    		     },
    		     {
    		    	 field: 'goodsName'
    		     },
    		     {
    		    	 field: 'oriPrice'
    		     },
    		     {
    		    	 field: 'conPrice'
    		     },
    		     {
    		    	 field: 'commissionProfit'
    		     },
    		     {
    		    	 field: 'commission'
    		     },
    		     {
    		    	 field: 'isBaokuan',
    		    	 render: function ( rowData ){
    		    		 if( "001"==rowData.isBaokuan ){
    		    			 return $.funcbar({
        		    			 data: permission,    //请求的数据
        		    			 type: 'text',     //类型
        		    			 display: 1,    //显示数量
        		    			 permit: ['${name_english}:delBaokuan'] //需要的权限按钮
        		    		 });
    		    		 }else{
    		    			 return $.funcbar({
        		    			 data: permission,    //请求的数据
        		    			 type: 'text',     //类型
        		    			 display: 1,    //显示数量
        		    			 permit: ['${name_english}:initBaokuan'] //需要的权限按钮
        		    		 }); 
    		    		 }
    		    	 }
    		     },
    		     {
    		    	 field: 'isJingxuan',
    		    	 render: function ( rowData ){
    		    		 if( "001"==rowData.isJingxuan ){
    		    			 return $.funcbar({
        		    			 data: permission,    //请求的数据
        		    			 type: 'text',     //类型
        		    			 display: 1,    //显示数量
        		    			 permit: ['${name_english}:delJingxuan'] //需要的权限按钮
        		    		 });
    		    		 }else{
    		    			 return $.funcbar({
        		    			 data: permission,    //请求的数据
        		    			 type: 'text',     //类型
        		    			 display: 1,    //显示数量
        		    			 permit: ['${name_english}:initJingxuan'] //需要的权限按钮
        		    		 });
    		    		 }
    		    	 }
    		     },
    		    {
    		    	 render: function (rowData) {
    		    		 return $.funcbar({
    		    			 data: permission,    //请求的数据
    		    			 type: 'text',     //类型
    		    			 display: 3,    //显示数量
    		    			 permit: ['${name_english}:initOrder','${name_english}:del'] //需要的权限按钮
    		    		 });
    		    	 }
    		    }
    		          
    	    ]
    	});
    }
    
    
    //表单提交
    function submitOrder(id,reorder) {
        $.ajax({
     	   url:basePath + "${common_url}order",
     	   data:{"id":id,"reorder":reorder},
     	   success:function(data){
     		   $.stip({
  	                type:'ok',//显示类型 有info、 ok、 danger
  	                text: '保存成功！',
  	                onBeforeHide: function () {
  	                	$(".maskback").hide();
  	                	//reloadTable();
  	  			        //  var nextTd = $.find('td[data-value="'+id+'"]');
  	  			        // var $this = $(this),
			   				 //id = $this.closest('tr').find('td[data-field="id"]').attr('data-value');
  	  			     
  	  			        //$(this).prevAll().next();
  	  			        $("#"+id).html('<div class="summer-datagrid-cell ">'+reorder+'</div>');
  	               }
              	});
     	   }
        });
    }
    
   
</script>
</body>
</html>